package com.ys.controller.web.sys;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.ys.config.MenuId;
import com.ys.entity.sys.TsUserInfo;
import com.ys.service.sys.CommonApiService;
import com.ys.util.OrderUtil;

import lombok.AllArgsConstructor;
import lombok.Data;

@Controller
@RequestMapping("/web/sys/tsUserInfo")
public class WebTsUserInfoController {
	
	@Autowired
	private CommonApiService<TsUserInfo> commonService;
	
	/**
	 * 新增
	 * @throws Exception 
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, @RequestParam Map<String, Object> params) throws Exception{
		
		// 计算下一个用户编号
		TsUserInfo max = new TsUserInfo().where("A.UserId >= '00000'")
				.where("A.UserId <= '08000'")
				.setOrderby("A.UserId desc")
				.queryFirst();
		String userId = "";
		if (max == null) {
			userId = "00000";
		} else {
			String maxUserId = max.getUserId();
			userId = OrderUtil.getUserCode(Integer.parseInt(maxUserId) + 1);
		}
		
		TsUserInfo entity = new TsUserInfo();
		entity.setUserId(userId);
		return commonService.toAdd(model, entity, MenuId.TS_UserInfo, null, null);
	}
	
	/**
	 * 编辑
	 * @throws Exception 
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model,Long id) throws Exception{
		Map<String,Object> extraParam = new TsUserInfo()
				.addField("U.UserSex AS userSex")//性别
				.addField("U.UserState AS userState")//状态
				.addField("BA1.HigherLevelPath + ',' + BA1.Name AS nativePlaceName")//籍贯
				.addField("BA2.HigherLevelPath + ',' + BA2.Name AS workCityName")//工作地址
				.addField("U.NativePlace AS nativePlace")//籍贯
				.addField("U.WorkCity AS workCity")//工作地址
				.addField("U.OfficeId AS officeId")//所属办公室Id
				.addField("B.VReMark AS officeName")//所属办公室地址
				.addField("U.HeadUserId AS userLeader")//用户上级工号
				.addField("U.RoleIdAttribute AS roleIdAttribute")//用户类型/岗位属性(0职能、1运营)
				.addLeftJoin("THR_UserArchives U ON U.UserId = A.UserId")
				.addLeftJoin("TB_BaseInfoArea BA1 ON BA1.Code = U.NativePlace AND 1=1")
				.addLeftJoin("TB_BaseInfoArea BA2 ON BA2.Code = U.WorkCity AND 1=1")
				.addLeftJoin("TB_BaseInfo B ON B.Id = U.OfficeId AND 1=1")
				
				.addLeftJoin("TB_CompanyInfo D ON D.Vid = A.CompanyVid")
				.addField("D.Vname companyVname")
				
				.addLeftJoin("TS_OrganizationInfo E ON E.Id = A.Orgid")
				.addField("E.DeptName deptName")
				.addField("E.DeptPath deptPath")
				.addField("E.DeptPathName deptPathName")
				
				.setId(id)
				.queryFirstMap();
		return commonService.toEdit(model, id, new TsUserInfo(), MenuId.TS_UserInfo, extraParam, null);
	}
	
	/**
	 * 重置密码
	 */
	@GetMapping("/toResetPass")
	public String toResetPass(Model model,
			Long id){
		
		TsUserInfo entity = new TsUserInfo().setId(id).queryFirst();
		String userId = entity.getUserId();
		String userName = entity.getUserName();
		ToResetPassDto dto = new ToResetPassDto(id, userId, userName);
		
		model.addAttribute("dto", dto);
		return "sys/tsUserInfo/toResetPass";
	}
	@Data
	@AllArgsConstructor
	private static class ToResetPassDto{
		private Long id; // GUID
		private String userId;
		private String userName; // 名称
	}
	
	/**
	 * 批量修改用户所属机构
	 */
	@GetMapping("/toBatchChangeOrg")
	public String toBatchChangeOrg(Model model,
			String ids){
		
		List<TsUserInfo> listTsUserInfo = new TsUserInfo().where("A.Id in (" + ids + ")").queryList();
		List<ToBatchChangeOrgDto.RowDto> rows = new ArrayList<ToBatchChangeOrgDto.RowDto>();
		for(TsUserInfo entity : listTsUserInfo) {
			Long id = entity.getId();
			String userId = entity.getUserId();
			String userName = entity.getUserName();
			rows.add(new ToBatchChangeOrgDto.RowDto(id, userId, userName));
		}
		
		ToBatchChangeOrgDto dto = new ToBatchChangeOrgDto(rows);
		
		model.addAttribute("dto", dto);
		return "sys/tsUserInfo/toBatchChangeOrg";
	}
	@Data
	@AllArgsConstructor
	private static class ToBatchChangeOrgDto{
		private List<RowDto> rows;
		@Data
		@AllArgsConstructor
		private static class RowDto{
			private Long id; // 
			private String userId;
			private String userName; // 名称
		}
	}
}
